package com.nil.codegen.exception.handler;

import com.nil.codegen.bean.ResponseResult;
import com.nil.codegen.enums.ResultCodeEnum;
import com.nil.codegen.exception.BusinessException;
import com.nil.codegen.exception.InvalidArgumentException;
import com.nil.codegen.exception.NullArgumentException;
import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.stream.Collectors;

/**
 *  全局异常处理
 * @author: Nil caoshi
 * @date: 2025-02-09 20:22:20
 * @since: 1.0
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 业务异常处理
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    public ResponseResult businessExceptionHandler(BusinessException e) {
        log.error(String.format("业务异常，详情：%s", e.getMsg()));
        return ResponseResult.error(e.getMsg());
    }


    /**
     * 参数校验异常处理
     */
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult exceptionHandler(MethodArgumentNotValidException e) {
        String errorMsgs = e.getBindingResult().getFieldErrors().stream()
                .map((item)-> String.format("[%s:%s]", item.getField(), item.getDefaultMessage()))
                .collect(Collectors.joining(";"));
        log.error("请求参数校验错误，原因为：{}", errorMsgs);
        return ResponseResult.error(ResultCodeEnum.INTERNAL_SERVER_ERROR.getRet(), String.format("请求参数校验错误,详情: %s", errorMsgs));
    }

    /**
     * 上传文件过大异常处理
     */
    @ResponseBody
    @ExceptionHandler(value = SizeLimitExceededException.class)
    public ResponseResult sizeLimitExceededException(SizeLimitExceededException e) {
        log.error("文件上传过大，最大值为{}，实际大小为{}", e.getPermittedSize(), e.getActualSize());
        return ResponseResult.error(ResultCodeEnum.INTERNAL_SERVER_ERROR.getRet(), "文件过大");
    }

    /**
     * 不支持的文件格式异常处理
     */
    @ResponseBody
    @ExceptionHandler(value = HttpMediaTypeNotSupportedException.class)
    public ResponseResult httpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        log.error("不支持的媒体格式{}，当前支持{}", e.getContentType(), e.getSupportedMediaTypes());
        return ResponseResult.error(ResultCodeEnum.INTERNAL_SERVER_ERROR.getRet(), "不支持的媒体格式");
    }

    /**
     * 其他异常处理
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseResult defaultHandler(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseResult.error(ResultCodeEnum.INTERNAL_SERVER_ERROR.getRet(), e.getMessage());
    }

    /**
     * 对象验证非法异常处理
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = InvalidArgumentException.class)
    public ResponseResult InvalidArgumentExceptionHandler(InvalidArgumentException e) {
        log.error(String.format("对象验证非法异常, 详情：%s", e.getMessage()));
        return ResponseResult.error(ResultCodeEnum.USERNAME_OR_PASSWORD_ERROR.getRet(), e.getMessage());
    }


    @ResponseBody
    @ExceptionHandler(value = NullArgumentException.class)
    public ResponseResult NullArgumentExceptionHandler(NullArgumentException e) {
        log.error(String.format("参数为空, 详情：%s", e.getMessage()));
        return ResponseResult.error(ResultCodeEnum.INTERNAL_SERVER_ERROR.getRet(), e.getMessage());
    }


}
